package mmc.ui;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mmc.action.*;
import mmc.resource.config.SavedVariables;


public class KeyBinding {
	public static Map<Integer, Action> binding = new HashMap<Integer, Action>();
	
	public KeyBinding()
	{
		init();
	}
	
	public static List<Action> getActions(){
		List<Action> l = new ArrayList<Action>();
		
		l.add(new BiggerViewRange());
		l.add(new LowerViewRange());
		l.add(new Target());
		l.add(new ToggleInterfaceVisibility());
		l.add(new ExitGame());
		l.add(new SelfTarget());
		l.add(new TargetTarget());
		
		return l;
	}
	
	public void init()
	{
		Map<Integer, String> map = SavedVariables.loadConfig();
		if(map != null)
		{
			for (Map.Entry<Integer, String> a : map.entrySet()){
				Action act = null;
				try {
					if(a.getValue().split("=").length == 2 && !a.getValue().split("=")[1].contains(" "))
						act = (Action) Class.forName(a.getValue().split("=")[1]).getConstructor().newInstance();
					else{
						String help = a.getValue().split("=")[1];
						act = (Action) Class.forName(a.getValue().split("=")[1].split(" ")[0]).getConstructor(String.class, String.class, Integer.TYPE).newInstance(help.split(" ")[1], null, a.getKey());
					}
				} catch (Exception e)
				{
					e.printStackTrace();
				}
				if(act != null)
					binding.put(a.getKey(), act);
			}
		}
		else
		{
			for (Action a : getActions()){
				binding.put(a.defaultKey, a);
			}
		}
	}
	
	public static void addWindowShortcut(String name, int key, Window window){
		OpenWindow help = new OpenWindow(name, null, key, window);
		if(!KeyBinding.binding.containsValue(help)){
			KeyBinding.binding.put(help.defaultKey, help);
		}
		else{
			ArrayList<Action> arr = new ArrayList<Action>( KeyBinding.binding.values());
			((OpenWindow)arr.get(arr.indexOf(help))).setCom(window);
			((OpenWindow)arr.get(arr.lastIndexOf(help))).setCom(window);
		}
	}
	
	public static void addBartenderShortcut(List<BartenderKey> keys, BartenderKey bart){
		if(!KeyBinding.binding.containsValue(bart)){
			KeyBinding.binding.put(bart.defaultKey, bart);
		}

		ArrayList<Action> arr = new ArrayList<Action>(KeyBinding.binding.values());
		keys.add((BartenderKey) arr.get(arr.indexOf(bart)));
	}
		/*
	public boolean changeValueOnKey(Integer key, Action skill)
	{
		boolean overwrite = false;
		if(binding.containsKey(key))
		{
			Action help = binding.get(key);
			int old_key=0;
			for (Integer sk : binding.keySet()) {
				if(skill == binding.get(sk))
				{
					old_key = sk;
					break;
				}
			}
			
			binding.put(key, skill);
			binding.put(old_key, help);
			overwrite = true;
		}
		else
		{
			if (binding.containsValue(skill))
			{
				for (Integer sk : binding.keySet()) {
					if(skill == binding.get(sk))
					{
						binding.remove(sk);
						break;
					}
				}
			}
			binding.put(key, skill);
		}
		return overwrite;
	}
*/
	public static void changeValueOnKey(int oldkey, int key, Action str) {		
		if(binding.get(oldkey) == str)
		{
			binding.remove(oldkey);
		}
		
		if(binding.containsKey(key))
		{			
			Action a = binding.get(key);
			binding.put(oldkey, a);				
			a.defaultKey = oldkey;
		}

		binding.put(key, str);
		str.defaultKey = key;
	}
	
	public static Action getSkill(String name)
	{
		for(Action a : binding.values())
		{
			if(a instanceof mmc.action.BartenderKey)
			{
				Action b = ((mmc.action.BartenderKey) a).action;
				if(b.getClass().getName().equals(name))
				{
					return b;
				}	
			}			
		}
		return null;
	}
}
